var vue = new Vue({
    el: '#app',
    data: {
        loginLoading: false,
        loginForm: {
            loginName: '',
            password: '',
        },
        loginRule: {
            loginName: [
                {validator: validateLoginName, trigger: 'blur'}
            ],
            password: [
                {validator: validatePassword, trigger: 'blur'}
            ],
        },
        loginNameMsg: '',
        passwordMsg: '',
    },
    computed: {
        loginBtnMsg: function () {
            if (this.loginLoading) {
                return '登录中...';
            } else {
                return '登  录';
            }
        },
    },
    methods: {
        resetValidate: function (msgKey) {
            if (this[msgKey] != '') {
                this[msgKey] = '';
                this.$refs['loginForm'].clearValidate();
            }
        },
        submitForm: function (formName) {
            var self = this;
            self.loginLoading = true;
            self.$refs[formName].validate(function (valid) {
                if (valid) {
                    var url = "/system/login/loginForLoginName?etc=" + new Date().getTime();
                    var data = {
                        "loginName": self.loginForm.loginName,
                        "password": md5(self.loginForm.password),
                    };
                    Util.post(url, data, function (result) {
                        Util.storage.set(Util.USER_INFO_KEY, result.obj);
                        if (result.success) {
                            Util.storage.removeRegular(GLOBAL_CONFIG.webSiteRoot);
                            top.location = GLOBAL_CONFIG.webSiteRoot + result.extend.homeUrl + "?ticketId=" + result.obj.ticketId;
                            return;
                        } else {
                            self.$message.error(result.msg);
                        }
                        self.loginLoading = false;
                    }, true);
                } else {
                    self.loginLoading = false;
                    return false;
                }
            });
        },
        resetForm: function (formName) {
            this.$refs[formName].resetFields();
        },
    }
});

function validateLoginName(rule, value, callback) {
    if (value === '') {
        callback(new Error('请输入登录名!'));
    } else {
        callback();
    }
}

function validatePassword(rule, value, callback) {
    if (value === '') {
        callback(new Error('请输入密码'));
    } else {
        callback();
    }
}
